﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Web;
using HtmlAgilityPack;

namespace GetRawWeb
{
    public class AbsWebReQuest
    {
        public Uri Uri { get; set; }
        public AbsWebReQuest(Uri uri)
        {
            Uri = uri;
            Content = GetContent(uri);
        }
        public string Content { get; set; }
        public string GetContent(Uri uri)
        {
            var wc = new WebClient();
            return HttpUtility.HtmlDecode(wc.DownloadString(uri));
        }

        public IEnumerable<LinkWeb> GetLinkWebs()
        {
            var htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(Content);
            var nodeACollection = htmlDocument.DocumentNode.SelectNodes("//a|//link|//img|//script|//style");
            if (nodeACollection != null)
                foreach (var node in nodeACollection)
                {
                    string href = null;
                    var type = TypePage.A;
                    switch (node.Name)
                    {
                        case "a":
                            var taga = node.Attributes["href"];
                            if(taga==null) continue;
                            href = taga.Value;
                            type = TypePage.A;
                            break;
                        case "link":
                            var linktag = node.Attributes["href"];
                            if(linktag==null)continue;
                            href = linktag.Value;
                            type = TypePage.Link;
                            break;
                        case "img":
                            var hreftag = node.Attributes["src"];
                            if(hreftag==null) continue;
                            href = hreftag.Value;
                            type = TypePage.Img;
                            break;
                        case "script":
                            var script = node.Attributes["src"];
                            if (script == null)
                                continue;
                            href = script.Value;
                            type = TypePage.Script;
                            break;
                        case "style":
                            var tagstyle = node.Attributes["href"];
                            if(tagstyle==null) continue;
                            href = tagstyle.Value;
                            type = TypePage.Style;
                            break;
                    }

                    if (string.IsNullOrEmpty(href) || Contant.StartWith(href)) continue;
                    if (!href.StartsWith(Uri.Scheme))
                    {
                        href = Uri.Scheme + "://" + Uri.Host + "/" + href;
                    }
                    if (href.StartsWith(Uri.Scheme + "://" + Uri.Host))
                    {
                        yield return new LinkWeb()
                        {
                            Uri = new Uri(href),
                            Type = type
                        };
                    }
                }
        }
    }
}
